<?php
namespace Modules\Comment\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Auth\Http\Resources\UserResource;
use Modules\Good\Http\Resources\GoodResource;

class CommentResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'good_id' => $this->good_id,
            'content' => $this->content,
            'rate' => $this->rate,
            'reply' => $this->reply,
            'created_at' => $this->created_at,
            // ✅ 用 whenLoaded 动态加载 user 和 goods 关联
            'user'      => new UserResource($this->whenLoaded('user')),
            'good'     => new GoodResource($this->whenLoaded('good')),
        ];
    }
    public function includeUser(){
        return $this->whenLoaded('user');
    }
    public function includeGoods(){
        return $this->whenLoaded('goods');
    }
    
}
